package com.cchip.cloud;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cchip.cloud.api.MeetingApi;
import com.cchip.cloud.api.MeetingReturned;
import com.cchip.cloud.system.Account;
import com.cchip.cloud.system.BackgroundRunner;
import com.cchip.cloud.system.Config;

public class EditPasswordActivity extends Activity {
	private static final int REFRESH_HINT_SUCCESS = 28;
	private static final int REFRESH_HINT_NOT_SAME = 29;
	private static final int REQ_SET_USER_PASSWORD = 15;

	private EditText mOldPwdEtxt = null;
	private EditText mNewPwdEtxt = null;
	private EditText mRepeatPwdEtxt = null;

	private MeetingApi mMeetingApi = MeetingApi.getInstance();
	private Requester mRequester = null;
	private Refresher mRefresher = null;

	private Activity context = null;
	private ProgressDialog mWaitDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;

		this.setContentView(R.layout.edit_pwd);
		this.setTitle("修改用户密码");

		mRequester = new Requester(BackgroundRunner.getRunner());
		mRefresher = new Refresher();

		mOldPwdEtxt = (EditText) this.findViewById(R.id.old_password);
		mNewPwdEtxt = (EditText) this.findViewById(R.id.new_password);
		mRepeatPwdEtxt = (EditText) this.findViewById(R.id.repeat_password);

		Button cancelBtn = (Button) this.findViewById(R.id.cancel);
		Button okBtn = (Button) this.findViewById(R.id.ok);

		cancelBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				context.finish();
			}

		});

		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showWaitDialog();
				String pwd = mNewPwdEtxt.getText().toString().trim();
				String repeat = mRepeatPwdEtxt.getText().toString().trim();

				if (pwd.equals(repeat)) {
					mRequester.sendEmptyMessage(REQ_SET_USER_PASSWORD);
				} else {
					mRefresher.sendEmptyMessage(REFRESH_HINT_NOT_SAME);
				}
			}

		});

	}

	private void showWaitDialog() {
		mWaitDialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
		mWaitDialog.setMessage("资料提交中...");
		mWaitDialog.setCancelable(false);
		mWaitDialog.show();
	}

	private class Requester extends Handler {

		public Requester(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			MeetingReturned ret = null;
			switch (msg.what) {
			case REQ_SET_USER_PASSWORD:
				String oldPwd = mOldPwdEtxt.getText().toString().trim();
				String newPwd = mNewPwdEtxt.getText().toString().trim();

				ret = mMeetingApi.setUserPassword(Account.my.name, oldPwd,
						newPwd);
				if (ret != null) {
					if (ret.operCode == MeetingReturned.CODE_OK) {
						mRefresher.sendEmptyMessage(REFRESH_HINT_SUCCESS);
						Account.my.password = newPwd;
						Config.setPassword(newPwd);
					} else {
						mRefresher.sendEmptyMessage(ret.operCode);
					}
				} else {
					mRefresher
							.sendEmptyMessage(MeetingReturned.CODE_NO_RESPONSE);
				}
				break;
			}
		}

	}

	private class Refresher extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);

			mWaitDialog.cancel();
			String hint = null;
			switch (msg.what) {
			case REFRESH_HINT_SUCCESS:
				hint = "密码修改成功";
				context.finish();
				break;
			case REFRESH_HINT_NOT_SAME:
				hint = "新密码两次输入不一致，请重新输入";
				break;
			case MeetingReturned.CODE_PASSWORD_WRONG:
				hint = "密码有误，请检查您的输入";
				break;
			case MeetingReturned.CODE_PASSWORD_INVALID:
				hint = "您的密码不符合要求，请更换再试";
				break;
			case MeetingReturned.CODE_ROOM_PASSOWRD_INVALID:
				hint = "会议室密码不符合要求，请更换再试";
				break;
			case MeetingReturned.CODE_NO_RESPONSE:
				hint = "服务器无响应，请稍后再试";
				break;
			default:
				hint = "密码修改失败，请重新输入";
				break;
			}

			if (hint != null) {
				Toast toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		}
	}

}
